var io = require('socket.io').listen(3000);
var clients = [];//存放建立连接的集合
var http = require("http");

http.createServer(function(request, response) {
  console.log("hellow world !");
  io.emit('receive',{
      text: 's'
  });
}).listen(8888);
io.sockets.on('connection', function (socket) {
     var socketId = socket.id;
     socket.on('join', function (data) {
        console.log("用户ID为:"+data.adminId + "socketI为:" + socketId);
        clients.push({ "socketId":socket.id,"adminId": data.adminId, "socket": socket});//成功建立连接以后,将新连接放在clients集合中
     });

     socket.on('disconnect', function () {//用户退出时从集合里面清除
        for (var i = 0; i < clients.length; i++) {
             if (clients[i].socketId == socketId) {
                 clients.splice(i, 1);
             }
        }
        console.log("用户退出链接" + socketId);
     });

     socket.on('send', function (data) {
     console.log("用户:"+data.username+"说:"+data.text);
     for (var i = 0; i < clients.length; i++) {
          var so = clients[i].socket;
          var adminId = clients[i].adminId;
          if (adminId == "5" || adminId == "1" || adminId == "7" || adminId == "6") {//给adminI的为5的用户推送
              so.emit('receive',{
                   text: data.text
              });
          }
     }
     });
});